package com.ymatou.topicrecbizer.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.topicrecbizer.parameter.NoteListBean;
import com.ymatou.topicrecbizer.service.NoteListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;

public class Ts_NoteList {
	private static NoteListBean notelistBean;
	private static NoteListCall notelistCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("笔记Topic详情列表接口");
	}

	@Before
	public void caseUp() {
		notelistBean = new NoteListBean();
		notelistCall = new NoteListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(notelistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_NoteList_001() {
		Logger.start(true,"笔记Topic详情列表接口-检查接口返回公共信息 ");
		try {
			//设置参数
			String requestid="111111";
			long timenow = new Date().getTime();
			String topicId = "23436";
			String score = "tpscore";
			
			notelistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			notelistBean.setQuery("term(topicId,"+topicId+")");
			notelistBean.setSort("desc("+score+")");
			notelistBean.setAppkey("1");
			notelistBean.setRequestid(requestid);
			notelistBean.setModule("homepage_topic_note_list");
			notelistBean.setPlatform("appbuyer");
			notelistBean.setLimit("0,10");
			
			notelistCall.setData(notelistBean);
			notelistCall.callService();
			
			Logger.verifyEquals("OK",notelistCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(notelistCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, notelistCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, notelistCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, notelistCall.getCommonInfo("requestid"), "验证requestid值是否为所传值");
			Logger.verifyNotNull(notelistCall.getCommonInfo("hostip"), "验证是否有返回服务器ip");
			Logger.verifyEquals(score, notelistCall.getCommonInfo("algorithm"), "验证返回排序方式是否与所传值相同");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_NoteList_002() {
		Logger.start(true,"笔记Topic详情列表接口-检查接口返回数据，笔记不会重复 ");
		try {
			//设置参数
			String requestid="111111";
			long timenow = new Date().getTime();
			String topicId = "23436";
			String score = "tpscore";
			
			notelistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			notelistBean.setQuery("term(topicId,"+topicId+")");
			notelistBean.setSort("desc("+score+")");
			notelistBean.setAppkey("1");
			notelistBean.setRequestid(requestid);
			notelistBean.setModule("homepage_topic_note_list");
			notelistBean.setPlatform("appbuyer");
			notelistBean.setLimit("0,10");
			
			notelistCall.setData(notelistBean);
			notelistCall.callService();
			
			JSONArray hitsAry = notelistCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String noteid = json.getString("noteid");
				
				if (list.contains(noteid) == true) {

					Logger.verifyEquals(false, list.contains(noteid), "验证第" + i+ "条返回数据的笔记id存在重复值" + noteid);

				}else{
					Logger.verifyEquals(false, list.contains(noteid), "验证第" + i+ "条返回数据的笔记id不存在重复值" + noteid);
					list.add(noteid);
				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_NoteList_003() {
		Logger.start(true,"笔记Topic详情列表接口-检查接口每条数据的返回值是否齐全，并且有有效值 ");
		try {
			//设置参数
			String requestid="111111";
			long timenow = new Date().getTime();
			String topicId = "23436";
			String score = "tpscore";
			
			notelistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			notelistBean.setQuery("term(topicId,"+topicId+")");
			notelistBean.setSort("desc("+score+")");
			notelistBean.setAppkey("1");
			notelistBean.setRequestid(requestid);
			notelistBean.setModule("homepage_topic_note_list");
			notelistBean.setPlatform("appbuyer");
			notelistBean.setLimit("0,10");
			
			notelistCall.setData(notelistBean);
			notelistCall.callService();
			
			Logger.verifyEquals("OK", notelistCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = notelistCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			//获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("content") , "验证第"+i+"条记录返回结果中有content这个key");
			  Logger.verifyEquals(true,infoAry.has("id") , "验证第"+i+"条记录返回结果中有id这个key");
			  Logger.verifyEquals(true,infoAry.has("noteid") , "验证第"+i+"条记录返回结果中有noteid这个key");
			  Logger.verifyEquals(true, infoAry.has("collectionnum"), "验证第"+i+"条记录返回结果中有collectionnum这个key");
			  Logger.verifyEquals(true, infoAry.has("commentnum"), "验证第"+i+"条记录返回结果中有commentnum这个key");
			  Logger.verifyEquals(true,infoAry.has("pics") , "验证第"+i+"条记录返回结果中有pics这个key");
			  Logger.verifyEquals(true,infoAry.has("userid") , "验证第"+i+"条记录返回结果中有userid这个key");
			  
			  
			  if (infoAry.has("content") && infoAry.has("id") && infoAry.has("noteid") && infoAry.has("collectionnum") && infoAry.has("commentnum") && 
						infoAry.has("pics")&& infoAry.has("userid")) {
					String content = null;
					String id = null;
					String noteid = null;
					String collectionnum = null;
					String commentnum = null;
					String pics = null;
					String userid = null;
					

					if (infoAry.get("id") != "0" && infoAry.get("id") != null) {
						id = infoAry.getString("id");
					}
					if (infoAry.get("content") != "0"
							&& infoAry.get("content") != null) {
						content = infoAry.getString("content");
					}
					if (infoAry.get("noteid") != "0"
							&& infoAry.get("noteid") != null) {
						noteid = infoAry.getString("noteid");
					}
					if (infoAry.get("collectionnum") != "0"
							&& infoAry.get("collectionnum") != null) {
						collectionnum = infoAry.getString("collectionnum");
					}
					if (infoAry.get("commentnum") != "0"
							&& infoAry.get("commentnum") != null) {
						commentnum = infoAry.getString("commentnum");
					}
					if (infoAry.get("pics") != "0"
							&& infoAry.get("pics") != null) {
						pics = infoAry.getString("pics");
					}
					if (infoAry.get("userid") != "0"
							&& infoAry.get("userid") != null) {
						userid = infoAry.getString("userid");
					}

					Logger.verifyNotNull(id, "验证第" + i + "条记录返回结果中有id不为空且不为0");
					Logger.verifyNotNull(content, "验证第" + i + "条记录返回结果中有content笔记内容不为空且不为0");
					Logger.verifyNotNull(noteid, "验证第" + i+ "条记录返回结果中有noteid笔记id不为空且不为0");
					Logger.verifyNotNull(collectionnum, "验证第" + i+ "条记录返回结果中有collectionnum收藏数不为空且不为0");
					Logger.verifyNotNull(commentnum, "验证第" + i+ "条记录返回结果中有commentnum评论数不为空且不为0");
					Logger.verifyNotNull(pics, "验证第" + i+ "条记录返回结果中有pics图片地址不为空且不为0");
					Logger.verifyNotNull(userid, "验证第" + i+ "条记录返回结果中有userid用户id不为空且不为0");
					
				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}
